package com.wardrobe.soa.backend.train_manage;

import com.google.common.collect.Maps;
import com.wardrobe.domain.banner.BannerBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.train_manage.TrainManageBean;
import com.wardrobe.domain.train_manage.TrainManageShowBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/15.
 */
@RestController
@RequestMapping("/backend/trainManage")
public class BackTrainManageController {
    private static final Logger logger = LoggerFactory.getLogger(BackTrainManageController.class);
    @Autowired
    private BackTrainManageService backTrainManageService;

    @RequestMapping("/trainManageList")
    public Map<String, Object> getTrainManageList(BackTrainManageBean manageBean) {
        Map<String, Object> result = Maps.newHashMap();
        List<TrainManageShowBean> list = backTrainManageService.getTrainManage(manageBean);
        list.stream().forEach(showBean -> {
            showBean.setCreateTime(showBean.getCreateTime().substring(0,showBean.getCreateTime().length()-2));
            showBean.setUpdateTime(showBean.getUpdateTime().substring(0,showBean.getUpdateTime().length()-2));
        });
        long count = backTrainManageService.countTrainManage(manageBean);
        int pageCount = (int) Math.ceil(((double) count / manageBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", manageBean.getPageSize());
        return result;
    }
    @RequestMapping("/getTrainManageById")
    public TrainManageShowBean getTrainManageById(int id){
        return backTrainManageService.getTrainManageById(id);
    }
    @RequestMapping("/modifyTrainManage")
    public Map<String,Object> modifyTrainManage(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backTrainManageService.modifyTrainManage(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyTrainManage error",e);
        }
        return result;
    }
    @RequestMapping("/addTrainManage")
    public Map<String,Object> addTrainManage(TrainManageBean trainManageBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            int status = backTrainManageService.addTrainManage(trainManageBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","添加成功");
            }else{
                result.put("status",2);
                result.put("msg","添加失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","添加失败");
            logger.error("addTrainManage error",e);
        }
        return result;
    }
    @RequestMapping("/deleteTrainManage")
    public int deleteTrainManage(int id){
        return backTrainManageService.deleteTrainManage(id);
    }
}
